package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping( "/depts")
@Slf4j
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    /*列表查询*/
    @GetMapping
    public Result list( String name,Integer status,
                       @RequestParam( defaultValue = "1") Integer page,
                       @RequestParam (defaultValue = "10") Integer pageSize){
        /*System.out.println("列表查询"+name+" "+ status+ " "+page+" "+ pageSize);
         * return Result.success();*/
        log.info("查询列表:{}{}{}{}",  name,status, page,pageSize);
        PageResult<Dept> pageResult = deptService.list(name, status, page,pageSize);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result addDept(@RequestBody Dept dept) {
        log.info("添加部门:{}",dept);
        deptService.addDept(dept);
        return Result.success();
    }
    @DeleteMapping ("/{id}" )
    public Result deleteDept(@PathVariable ("id") Integer id){
        log.info("删除部门:{}",id);
        deptService.deleteDept( id);
        return Result.success();
    }
    @GetMapping ("/{id}" )
    public  Result getDeptById( @PathVariable ("id")Integer  id){
        log.info("查询部门{}:",id);
        Dept dept = deptService.getDeptById(id);
        return Result.success(dept);
    }
    @PutMapping
    public Result updateDept(@RequestBody Dept dept){
        log.info("更新部门:{}",dept);
        deptService.updateDept( dept);
        return Result.success();
    }
    @GetMapping("/list")
    //查询所有状态正常的部门
    public Result getAllDept(){
        log.info("查询所有部门");
        List<Dept> deptList = deptService.getAllDept();
        return Result.success(deptList);
    }


}
